import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class BorderTimeLine extends BorderDirectional {
  int position;
  int count;

  BorderTimeLine(this.position, this.count);

  double radius = 8.0.r;
  double ringRadius = 12.0.r;
  double margin = 0;
  final Paint _paint = Paint()
    ..color = Colors.red
    ..strokeWidth = 1;

  final Paint _gradientPaint = Paint()
    ..shader = ui.Gradient.linear(
      const Offset(0, 0),
      Offset(32.r, 0),
      <Color>[
        const Color(0xFF0A4EF1),
        const Color(0xFF7AE0E7),
      ],
      [
        0.0,
        0.5,
      ],
    );

  @override
  void paint(Canvas canvas, Rect rect,
      {TextDirection? textDirection,
      BoxShape shape = BoxShape.rectangle,
      BorderRadius? borderRadius}) {
    if (position == count - 1) {
      if (count != 1) {
        canvas.drawLine(
            Offset(rect.left + margin + ringRadius / 2, rect.top),
            Offset(
                rect.left + margin + ringRadius / 2, rect.top + ringRadius * 2),
            _strokePaint());
      }
      canvas.drawCircle(
        Offset(rect.left + margin + ringRadius / 2, rect.top + ringRadius * 2),
        ringRadius,
        _lastRingPaint(),
      );

      canvas.drawCircle(
          Offset(
              rect.left + margin + ringRadius / 2, rect.top + ringRadius * 2),
          radius,
          _lastCirclePaint());
    } else if (position == 0) {
      canvas.drawLine(
          Offset(
              rect.left + margin + ringRadius / 2, rect.top + ringRadius * 2),
          Offset(rect.left + margin + ringRadius / 2, rect.bottom),
          _strokePaint());
      canvas.drawCircle(
        Offset(rect.left + margin + ringRadius / 2, rect.top + ringRadius * 2),
        radius,
        _fillPaint(),
      );
    } else {
      canvas.drawLine(
        Offset(rect.left + margin + ringRadius / 2, rect.top),
        Offset(rect.left + margin + ringRadius / 2, rect.bottom),
        _strokePaint(),
      );
      canvas.drawCircle(
        Offset(rect.left + margin + ringRadius / 2, rect.top + ringRadius * 2),
        radius,
        _fillPaint(),
      );
    }
  }

  Paint _fillPaint() {
    _paint.color = const Color(0xFFF0F2F7);
    _paint.style = PaintingStyle.fill;
    return _paint;
  }

  Paint _strokePaint() {
    _paint.color = const Color(0xFFF0F2F7);
    _paint.style = PaintingStyle.stroke;
    return _paint;
  }

  Paint _lastCirclePaint() {
    _gradientPaint.style = PaintingStyle.fill;
    return _gradientPaint;
  }

  Paint _lastRingPaint() {
    _gradientPaint.style = PaintingStyle.stroke;
    return _gradientPaint;
  }
}
